package games.ThunderFighter;

import javax.swing.*;

/**
 * Created by Administrator on 2016/12/13.
 */
public class RunGame {

    Enemy enemy;
    Myplane plane;
    static int grade=0;
    GameWindow window;

    RunGame(GameWindow window){
        this.window=window;
        enemy =new Enemy(window);
        plane=new Myplane(window);

        while(true){
            for(int i=0;i<20;i++){
                if ( Bullet.Xtrack[i]<=Enemy.x+60  &&  Bullet.Xtrack[i]>=Enemy.x  &&  Bullet.Ytrack[i]<=Enemy.y+30  && Bullet.Ytrack[i]>=Enemy.y){
                    Bullet.Xtrack[i]=1000;
                    Bullet.Ytrack[i]=1000;
                    Enemy.HP--;
                    if(Enemy.HP<=0){
                        Bullet.getDrop();
                        Enemy.x=(int)(Math.random()*340/10)*10;
                        Enemy.y=-40;
                        Enemy.enemy.setBounds(Enemy.x,Enemy.y,60,30);
                        Enemy.HP=5;
                        grade++;
                        window.label.setText("分数:"+grade);
                    }
                }
                if ( Bullet.Wtrack[i]<Myplane.x+60  &&  Bullet.Wtrack[i]>=Myplane.x  &&  Bullet.Ztrack[i]>Myplane.y+8  && Bullet.Ztrack[i]<Myplane.y+30){
                    Myplane.attack.stop();
                    Myplane.timeup.stop();
                    Myplane.timedown.stop();
                    Myplane.timeleft.stop();
                    Myplane.timeright.stop();
                    Enemy.time1.stop();
                    Enemy.attack.stop();
                    JOptionPane.showMessageDialog(window, "游戏结束");
                    this.newGame(window);
                }
            }

            for(int i=0;i<=2;i++){
                if ( Bullet.Xdroptrack[i]<Myplane.x+60  &&  Bullet.Xdroptrack[i]>=Myplane.x  &&  Bullet.Ydroptrack[i]>Myplane.y+8  && Bullet.Ydroptrack[i]<Myplane.y+30){
                    Bullet.Xdroptrack[i]=1000;
                    Bullet.Ydroptrack[i]=1000;
                    Bullet.drop[i].setBounds(Bullet.Xdroptrack[i],Bullet.Ydroptrack[i],8,8);
                    Myplane.changeBullet();
                }
            }
        }
    }


    public static void newGame(GameWindow window){
        Myplane.myplane.setBounds(150,550,60,30);
        Enemy.enemy.setBounds((int)(Math.random()*340/10)*10,-40,60,30);
        Myplane.x=150;
        Myplane.y=550;
        Myplane.keyCount=0;
        Myplane.bullet=0;
        Enemy.y=-40;
        grade=0;
        window.label.setText("分数:"+grade);

        Myplane.attack.stop();
        Myplane.timeup.stop();
        Myplane.timedown.stop();
        Myplane.timeleft.stop();
        Myplane.timeright.stop();
        Enemy.time1.stop();
        Enemy.attack.stop();
        Bullet.time1.stop();
        Bullet.time2.stop();
        Bullet.time3.stop();

        for(int i=0;i<=2;i++){
            Bullet.Xdroptrack[i]=1000;
            Bullet.Ydroptrack[i]=1000;
            Bullet.drop[i].setBounds(Bullet.Xdroptrack[i],Bullet.Ydroptrack[i],8,8);
        }

        for(int i=0;i<20;i++){
            Bullet.primaryBullet[i].setBounds(1000,1000,8,8);
            Bullet.enemyBullet[i].setBounds(1000,1000,8,8);
            Bullet.Xtrack[i]=1000;
            Bullet.Ytrack[i]=1000;
            Bullet.Wtrack[i]=1000;
            Bullet.Ztrack[i]=1000;
        }
        window.repaint();
    }
}
